#########
### Deaths and change points
### Manuscript

div.plotting <- function(mydata, divdata, area, time, lower, upper,y){
  ggplot(mydata, aes(x=time, y=d)) +
    geom_line() + 
    sapply(mydata$date[divdata[-c(1,length(divdata))]], function(xint2) geom_vline(aes(xintercept = xint2, color="Change Point"),linetype = "dashed")) +
    #  sapply(mydata$date[(1:1430)[agree$agree==1]], function(xint) geom_vline(aes(xintercept = xint, color="Meeting"),linetype = "dashed")) + 
    ggtitle(area) +  theme_classic() + 
    scale_y_continuous(name=y, limits=c(lower, upper)) + 
    labs(x = " ", y = y, color = " ", size=0.8) +
    scale_color_manual(name="", values = c("Change Point"="palevioletred2", "Meeting"="skyblue2")) +
    theme(plot.title = element_text(size = 16, family = "LM Roman 10"), 
          axis.text=element_text(size=10, family = "LM Roman 10"),
          axis.title=element_text(size=12, family = "LM Roman 10"),
          legend.text=element_text(size=12, family = "LM Roman 10"),
          legend.title=element_text(size=12, family = "LM Roman 10"))
}

p.as.op <- div.plotting(civil.as.op,divisive.as.op,"Opposition Territory",time=civil.as.op$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.op.as <- div.plotting(civil.op.as,divisive.op.as,"Government Territory",time=civil.op.as$date,-140,110,"Opposition-Inflicted")+theme(legend.direction = "horizontal")

p.as.is <- div.plotting(civil.as.is,divisive.as.is,"ISIS Territory",time=civil.as.is$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.is.as <- div.plotting(civil.is.as,divisive.is.as,"Government Territory",time=civil.is.as$date,-140,110,"ISIS Inflicted")

p.sdf.is <- div.plotting(civil.sdf.is,divisive.sdf.is,"ISIS Territory",time=civil.sdf.is$date,-140,110,"SDF-Inflicted") + theme(legend.position = "none")
p.is.sdf <- div.plotting(civil.is.sdf,divisive.is.sdf,"SDF Territory",time=civil.is.sdf$date,-140,110,"ISIS Inflicted") 

mylegend<-g_legend(p.op.as)

B <- grid.arrange(arrangeGrob(p.as.op,p.op.as+ theme(legend.position = "none"), 
                              p.as.is,p.is.as+ theme(legend.position = "none"), 
                              p.sdf.is,p.is.sdf+ theme(legend.position = "none"),ncol=2),mylegend, nrow=2,heights=c(10, 1))

ggsave("change_only.pdf", plot = B, width=9.83, height=9,units = "in")  

